<template>
  <div class="package-status-filter">
    <el-radio-group 
      v-model="selectedStatus" 
      @change="handleStatusChange"
      class="status-filter-group"
    >
      <el-radio-button label="">全部 ({{ totalCount }})</el-radio-button>
      <el-radio-button label="滞留件" class="retention-filter">
        滞留 ({{ retentionCount }})
      </el-radio-button>
      <el-radio-button label="待取件">待取件 ({{ pendingCount }})</el-radio-button>
      <el-radio-button label="已取件">已取件 ({{ completedCount }})</el-radio-button>
      <el-radio-button label="已完结">已完结 ({{ finishedCount }})</el-radio-button>
    </el-radio-group>
  </div>


  <el-table :data="tableData" style="width: 100%">
    <el-table-column label="包裹ID" width="180">
      <template #default="scope">
        <div style="display: flex; align-items: center">
          <el-icon><timer /></el-icon>
          <span style="margin-left: 10px">{{ scope.row.packageId }}</span>
        </div>
      </template>
    </el-table-column>

    <el-table-column label="订单号" width="180">
      <template #default="scope">
        <div style="display: flex; align-items: center">
          <el-icon><timer /></el-icon>
          <span style="margin-left: 10px">{{ scope.row.orderNo }}</span>
        </div>
      </template>
    </el-table-column>

    <el-table-column label="包裹单号" width="180">
      <template #default="scope">
        <div style="display: flex; align-items: center">
          <el-icon><timer /></el-icon>
          <span style="margin-left: 10px">{{ scope.row.packageNo }}</span>
        </div>
      </template>
    </el-table-column>


    <el-table-column label="存件快递员ID" width="180">
      <template #default="scope">
        <div style="display: flex; align-items: center">
          <el-icon><timer /></el-icon>
          <span style="margin-left: 10px">{{ scope.row.courierId }}</span>
        </div>
      </template>
    </el-table-column>

    <el-table-column label="存储格口ID" width="180">
      <template #default="scope">
        <div style="display: flex; align-items: center">
          <el-icon><timer /></el-icon>
          <span style="margin-left: 10px">{{ scope.row.gridId }}</span>
        </div>
      </template>
    </el-table-column>

    <el-table-column label="收件人ID" width="180">
      <template #default="scope">
        <div style="display: flex; align-items: center">
          <el-icon><timer /></el-icon>
          <span style="margin-left: 10px">{{ scope.row.recipientId }}</span>
        </div>
      </template>
    </el-table-column>

    <el-table-column label="派件时间" width="180">
      <template #default="scope">
        <div style="display: flex; align-items: center">
          <el-icon><timer /></el-icon>
          <span style="margin-left: 10px">{{ scope.row.deliveryTime }}</span>
        </div>
      </template>
    </el-table-column>

    <el-table-column label="包裹状态：待取件、已取件、滞留件、已取消、已完结" width="180">
      <template #default="scope">
        <div style="display: flex; align-items: center">
          <el-icon><timer /></el-icon>
          <span style="margin-left: 10px">{{ scope.row.packageStatus }}</span>
        </div>
      </template>
    </el-table-column>

    <el-table-column label="滞留天数（超过1天为滞留）" width="180">
      <template #default="scope">
        <div style="display: flex; align-items: center">
          <el-icon><timer /></el-icon>
          <span style="margin-left: 10px">{{ scope.row.滞留Days }}</span>
        </div>
      </template>
    </el-table-column>

    <el-table-column label="创建时间" width="180">
      <template #default="scope">
        <div style="display: flex; align-items: center">
          <el-icon><timer /></el-icon>
          <span style="margin-left: 10px">{{ scope.row.createTime }}</span>
        </div>
      </template>
    </el-table-column>

    <el-table-column label="更新时间" width="180">
      <template #default="scope">
        <div style="display: flex; align-items: center">
          <el-icon><timer /></el-icon>
          <span style="margin-left: 10px">{{ scope.row.updateTime }}</span>
        </div>
      </template>
    </el-table-column>





    <el-table-column label="Operations">
      <template #default="scope">
        <el-button size="small" @click="handleEdit(scope.$index, scope.row)"
        >Edit</el-button
        >
        <el-button
            size="small"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
        >Delete</el-button
        >
      </template>
    </el-table-column>
  </el-table>

</template>

<script setup>
import { ref, computed } from 'vue'
import axios from "axios";

const props = defineProps({
  packageList: {
    type: Array,
    default: () => []
  },
  modelValue: {
    type: String,
    default: ''
  }
})
let cabinet=ref({})
let total=ref()
let tableData=ref([])
const getCourierList = () => {
  axios.get("/api/system/package/getRetentionPackageList/").then(
      res=>{
        if (res.data.code==200){
          tableData.value=res.data.data.list
          total.value=res.data.data.table
        }
      }
  )
}
getCourierList()


const emit = defineEmits(['update:modelValue', 'change'])

const selectedStatus = ref(props.modelValue)

// 计算各状态数量
const totalCount = computed(() => props.packageList.length)
const retentionCount = computed(() => 
  props.packageList.filter(item => item.packageStatus === '滞留件').length
)
const pendingCount = computed(() => 
  props.packageList.filter(item => item.packageStatus === '待取件').length
)
const completedCount = computed(() => 
  props.packageList.filter(item => item.packageStatus === '已取件').length
)
const finishedCount = computed(() => 
  props.packageList.filter(item => item.packageStatus === '已完结').length
)

const handleStatusChange = (value) => {
  emit('update:modelValue', value)
  emit('change', value)
}
</script>

<style lang="scss" scoped>
.package-status-filter {
  margin-bottom: 20px;
}

.status-filter-group {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;

  :deep(.el-radio-button) {
    margin-right: 0;
  }

  :deep(.retention-filter .el-radio-button__inner) {
    border-color: #f56c6c;
    color: #f56c6c;

    &:hover {
      color: #f56c6c;
      border-color: #f56c6c;
    }
  }

  :deep(.retention-filter.is-active .el-radio-button__inner) {
    background-color: #f56c6c;
    border-color: #f56c6c;
    color: #fff;
  }
}
</style>